﻿#region Copyright and License Notice
// Copyright (C)2010-2016 - Rob Levine and other contributors
// http://configgen.codeplex.com
// 
// This file is part of ConfigGen.
// 
// ConfigGen is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// ConfigGen is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License and 
// the GNU Lesser General Public License along with ConfigGen.  
// If not, see <http://www.gnu.org/licenses/>
#endregion

using System.IO;
using ConfigGen.Utilities.Streams;
using Moq;
using NUnit.Framework;

namespace ConfigGen.Utilities.Tests.Streams
{
    [TestFixture]
    public class PauseableWritableStreamTests
    {
        [Test]
        public void PassThroughTest001_CanRead()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.CanRead).Returns(true);
            
            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            Assert.IsTrue(streamUnderTest.CanRead);
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest002_CanWrite()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.CanWrite).Returns(true);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            Assert.IsTrue(streamUnderTest.CanWrite);
            mockUnderlyingStream.Verify();
        }
        
        [Test]
        public void PassThroughTest003_CanSeek()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.CanSeek).Returns(true);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            Assert.IsTrue(streamUnderTest.CanSeek);
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest004_Flush()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.Flush());

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            streamUnderTest.Flush();
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest005_Length()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.Length).Returns(5);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            Assert.AreEqual(5, streamUnderTest.Length);
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest006_GetPosition()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.Position).Returns(5);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            Assert.AreEqual(5, streamUnderTest.Position);
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest007_SetPosition()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.SetupSet(s => s.Position = 5);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            streamUnderTest.Position = 5;
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest008_Read()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            var buffer = new byte[100];
            mockUnderlyingStream.Setup(s => s.Read(buffer, 5, 10)).Returns(20);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            var read = streamUnderTest.Read(buffer, 5, 10);
            Assert.AreEqual(20, read);
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest009_Seek()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.Seek(5, SeekOrigin.End)).Returns(3);

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            var ret = streamUnderTest.Seek(5, SeekOrigin.End);
            Assert.AreEqual(3, ret);
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest010_SetLength()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            mockUnderlyingStream.Setup(s => s.SetLength(66));

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            streamUnderTest.SetLength(66);
      
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest011_Write()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            var buffer = new byte[100];
            mockUnderlyingStream.Setup(s => s.Write(buffer, 5, 10));

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            streamUnderTest.Write(buffer, 5, 10);
            
            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest012_Write_PausePreventWriteToUnderlying()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            var buffer = new byte[100];

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            streamUnderTest.PauseWriting();

            streamUnderTest.Write(buffer, 5, 10);

            mockUnderlyingStream.Verify();
        }

        [Test]
        public void PassThroughTest013_Write_ResumeAfterPause()
        {
            var mockUnderlyingStream = new Mock<Stream>(MockBehavior.Strict);
            var buffer = new byte[100];
            mockUnderlyingStream.Setup(s => s.Write(buffer, 5, 10));

            var streamUnderTest = new PauseableWriteableStream(mockUnderlyingStream.Object);
            streamUnderTest.PauseWriting();
            streamUnderTest.ResumeWriting();

            streamUnderTest.Write(buffer, 5, 10);

            mockUnderlyingStream.Verify();
        }
    }
}
